#include <config.h>
#include <core.h>

#include <sys/ipc.h>
#include <sys/shm.h>



msgd_int_t msgd_shm_alloc(msgd_shm_t *shm)
{
   int fd;
   
   fd = shmget(IPC_PRIVATE,shm->size,(SHM_R|SHM_W|IPC_CREAT));
  
   if(fd == -1 ){
      ERROR("shmget failed");
      return MSGD_ERROR; 
   }

   shm->addr = shmat(fd,NULL,0);
   if(shm->addr == (void*)-1){
      ERROR("shmat failed");
   }


   if(shmctl(fd,IPC_RMID,NULL) == -1)
      ERROR("shmctl(IPC_RMID) failed");

   return (shm->addr == (void*)-1) ? MSGD_ERROR : MSGD_OK; 
}


void       msgd_shm_free(msgd_shm_t *shm)
{

    if(shmdt(shm->addr) == -1)
       ERROR("shmdt failed");
}
